#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "hash.h"

int testhash1()
{
	struct Hash *hash = CreateHash(0, 0, NULL);
	if (!hash) {
		printf("CreateHash failed\n");
		exit(1);
	}
	HashStat(hash);
	int ret = 0;
	char *v1 = (char*)malloc(20);
	strcpy(v1, "bbbbbbb");
	char *v2 = (char*)malloc(20);
	strcpy(v2, "cccccccc");
	ret  = Put( hash, "aaaaa", v1, strlen(v1), NULL);
	void *p = NULL;
	long v_len;
	ret = Get(hash, "aaaaa", &p, &v_len);
	printf("%d %s\n", v_len, p);
	HashStat(hash);
	ret = Set(hash, "aaaaa", v2, strlen(v2), NULL);
	ret = Get(hash, "aaaaa", &p, &v_len);
	printf("%d %s\n", v_len, p);
	HashStat(hash);

	return 0;
}
int testadd()
{
	struct Hash *hash = CreateHash(1010, 0, NULL);
	if (!hash) {
		printf("CreateHash failed\n");
		exit(1);
	}
	HashStat(hash);
	int ret = 0;
	int i;
	for (i = 0; i < 1001; i++) {
		char *v1 = (char*)malloc(101);
		sprintf(v1, "value%d", i);
		char key[100];
		memset(key, 0x00, sizeof(key));
		sprintf(key, "key%d", i);
		ret  = Put( hash, key, v1, strlen(v1), NULL);
		HashStat(hash);
	}
//
//	HashDetail(hash);
	for (i = 0; i < 1001; i++) {
		char key[100];
		memset(key, 0x00, sizeof(key));
		sprintf(key, "key%d", i);
		void *p = NULL;
		unsigned long v_len;
		Get(hash, key, &p, &v_len);
		printf("key %s val %d\n", p, v_len);
		//Contain(hash, key);
	}

	for (i = 0; i < 1001; i++) {
		char key[100];
		memset(key, 0x00, sizeof(key));
		sprintf(key, "key%d", i);
		void *p = NULL;
		unsigned long v_len;
		Delete(hash, key);
		HashStat(hash);
		//Contain(hash, key);
	}

	return 0;
}
char *getstr(char *a)
{
	char *x = NULL;
	x = malloc(21);
	memset(x, 0x00, 21);
	strcpy(x, a);
	return x;
}
void testbhash()
{
	struct BHash *bhash = NULL;
	bhash = CreateBHash(0, NULL);
BHashDetail(bhash);
	BPut(bhash, "aaa", getstr("aaa"), sizeof("aaa"), NULL);
BHashDetail(bhash);
	SetAllowMuti(bhash, HASH_ALLOWMUTI);
	BPut(bhash, "bbb", getstr("bbb"), sizeof("aaa"), NULL);
BHashDetail(bhash);
	BDelete(bhash, "aaa");
BHashDetail(bhash);
}
int main()
{
	testbhash();
	return 0;
}

